package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"wechat1/models/mysql"

	"wechat1/wechat1rpc/internal/svc"
	"wechat1/wechat1rpc/wechat1rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type PerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PerfectLogic {
	return &PerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PerfectLogic) Perfect(in *wechat1rpc.PerfectRequest) (*wechat1rpc.PerfectResponse, error) {
	// todo: add your logic here and delete this line
	var FindWechat mysql.Wechat
	mysql.Db.Debug().Where("mobile", in.Mobile).Find(&FindWechat)
	if FindWechat.ID == 0 {
		return nil, fmt.Errorf("用户已注册，请重新输入")
	}
	wechat := mysql.Wechat{
		Model:    gorm.Model{ID: uint(in.Id)},
		Mobile:   in.Mobile,
		Avatar:   in.Avatar,
		Nickname: in.Nickname,
		Address:  in.Address,
	}
	mysql.Db.Debug().Model(&wechat).Updates(&wechat)
	return &wechat1rpc.PerfectResponse{Id: int64(wechat.ID)}, nil
}
